from rest_framework.permissions import AllowAny
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
from rest_framework_simplejwt.views import TokenObtainPairView

from labor_price.models import People


class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
    def validate(self, attrs):
        data = super().validate(attrs)
        data['username'] = self.user.first_name + self.user.last_name
        data['id'] = self.user.id
        # data['depart'] = self.user.id
        # lists = list(People.objects.filter(username=self.user.username).values("Role__authoritys__url",
        #                                                                        "Role__authoritys__title"))
        # data["auth"] = lists
        roles = list(People.objects.filter(username=self.user.username).values("Role__jobName"))
        data["role"] = roles
        return data


class MyTokenObtainPairView(TokenObtainPairView):
    permission_classes = [AllowAny]

    serializer_class = MyTokenObtainPairSerializer
